package com.opcode.costa.ui.dialogs;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.opcode.costa.R;
import com.opcode.nebulamobil.base.activities.FragmentActivityBase;
import com.opcode.nebulamobil.base.components.ButtonBase;
import com.opcode.nebulamobil.base.components.LinearLayoutBase;
import com.opcode.nebulamobil.base.components.ListViewBase;
import com.opcode.nebulamobil.base.components.TextViewBase;
import com.opcode.nebulamobil.base.fragments.dialogfragments.DialogFragmentBase;
import com.opcode.nebulamobil.base.interfaces.OnButtonBaseClickListener;
import com.opcode.nebulamobil.base.interfaces.OnFoodDetailSelectListener;
import com.opcode.nebulamobil.base.interfaces.OnListViewBaseItemClickListener;
import com.opcode.nebulamobil.information.ApplicationInfo;
import com.opcode.nebulamobil.information.dataforms.costadataforms.FoodDataForm;


/**
 * 
 * Bu s�n�f, herhangi bir yemek tipindeki yemekler i�erisinden se�im yapmak i�in kullan�lacakt�r.
 * @author Okan ERDO�AN
 *
 * 10 Tem 2014
 */
public class FoodSelectingDialogFragment extends DialogFragmentBase{
	
	private View vi;
	
	/*
	 * Se�ili olan yemek tipine ait yemeklerin listesidir.
	 */
	private ArrayList<FoodDataForm> foodArray;
	
	/*
	 * Yemek listesindeki yemekleri listview i�erisinde g�stermek i�in kullan�lacak olan adapter nesnesidir.
	 */
	private FoodListAdapter foodListAdapter;
	
	/*
	 * Yemekler bu listview sayesinde kullan�c�ya bir liste i�erisinde g�sterilirler.
	 */
	private ListViewBase food_selection_listview;
	
	/*
	 * A��lan listeden se�imi yap�lan yemekler bu listede tutulur.
	 */
	private ArrayList<FoodDataForm> foodSelectedList;
	
	/*
	 * Bu intent nesnesi listeden herhangi bir yemek se�ildi�inde o yeme�e ait porsiyon se�imini yapacak olan ekran� a�mada kullan�lacakt�r.
	 */
	//private Intent intent;
	
	/*
	 * 
	 */
	private ButtonBase foodSelectingOkButton;
	
	/*
	 * 
	 */
	private int foodCategorie;
	
	private ButtonBase foodChoosingHeaderButton;
	
	private Handler myHandler = null;
	private Runnable myRunnable = null;
	
	private static boolean isEnable;
	
	/**
	 * Yap�land�r�c�s�nda yemeklere ait bir liste ve bu listenin kategorisini bildiren bir parametre ol�r.
	 * Buna g�re elde edilen listenini ana yemeklere mi, �orbalara vb. ait oldu�unu bilebiliriz.
	 * @param foodArray
	 * @param foodCategorie
	 */
	public  FoodSelectingDialogFragment(ArrayList<FoodDataForm> foodArray,int foodCategorie){
		this.foodArray=foodArray;
		this.foodSelectedList=new ArrayList<FoodDataForm>();
		
		for(int i=0;i<foodArray.size();i++){
			if(foodArray.get(i).isSelected()){
				foodSelectedList.add(foodArray.get(i));
			}
		}

		this.foodCategorie=foodCategorie;
	}
	
	
	
	/**
	 * Gerekli nesneleri referanlara atadığımız bölümdğr. Fragment ilk oluşturulduğu esnada çalıştırılır.
	 */
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		
		this.vi = inflater.inflate(R.layout.food_selecting_dialog_fragment_layout, container,false);
				
		this.foodChoosingHeaderButton=(ButtonBase) this.vi.findViewById(R.id.main_food_choosing_header_button);
		
		switch(this.foodCategorie){
			case 1:	foodChoosingHeaderButton.setText("ANA YEMEKLER");
					break;
			case 2:	foodChoosingHeaderButton.setText("ÇORBALAR");
					break;
			case 3:	foodChoosingHeaderButton.setText("İÇECEKLER");
					break;
			case 4:	foodChoosingHeaderButton.setText("TATLILAR");
					break;
		}
		
		this.food_selection_listview=(ListViewBase) this.vi.findViewById(R.id.food_selection_listview);

		this.foodListAdapter=new FoodListAdapter(this.getActivity(),R.layout.food_choosing_item,foodArray);
		this.food_selection_listview.setAdapter(foodListAdapter);
		
		this.food_selection_listview.setOnListViewBaseItemClickListener(new OnListViewBaseItemClickListener() {
			
			@Override
			public void onListViewItemClick(AdapterView<?> parent, View view,int position, long id) {
				
				/*
				 * Açılan yemek porsiyon ekranından seçimi yapılan porsiyon bilgisi ile beraber yemek listeye kayıt edilir.
				 */
				OnFoodDetailSelectListener foodDetailSelectListener=new OnFoodDetailSelectListener() {

					@Override
					public void onFoodDetailSelected(FoodDataForm foodDataForm) {
						
						boolean isAddedBefore=false;
						foodDataForm.setSelected(true);
						for(int i=0;i<foodSelectedList.size();i++){
							if(foodSelectedList.get(i).getFoodName().equals(foodDataForm.getFoodName())){
								if(foodDataForm.getCount()!=0){
									foodSelectedList.set(i, foodDataForm);	
								}else{
									foodSelectedList.remove(i);
								}
																	
								isAddedBefore=true;										
							}
						}
						
						if(!isAddedBefore){
							if(foodDataForm.getCount()!=0){
								foodSelectedList.add(foodDataForm);	
							}
						}
						
						for(int j=0;j<foodArray.size();j++){
							if(foodArray.get(j).getFoodName().equals(foodDataForm.getFoodName())){
								foodArray.set(j, foodDataForm);
							}
						}
						foodListAdapter.notifyDataSetChanged();
					}
				};
				
				ApplicationInfo.setOnFoodDetailSelectedListener(foodDetailSelectListener);
				
				 FoodDetailDialogFragment foodDetailDialogFragment=new FoodDetailDialogFragment(((FoodDataForm)food_selection_listview.getItemAtPosition(position)));
				 
				 //TODO BURAYI TEKRAR KONTROL ET******
				((FragmentActivityBase)getActivity()).showGeneralDialog(foodDetailDialogFragment, "FOOD_DETAIL_DIALOG");	
			}
		});

		this.foodSelectingOkButton=(ButtonBase) this.vi.findViewById(R.id.food_selecting_ok_button);
		this.foodSelectingOkButton.setWaitingState(false);
		this.foodSelectingOkButton.setOnButtonBaseClickListener(new OnButtonBaseClickListener() {
			
			@Override
			public void onButtonBaseClick(View v) {
				ApplicationInfo.getSelectedFoodListener().foodSelected(foodSelectedList, foodCategorie);
				dismiss();

			}
		});
		
		myHandler = new Handler();

		myRunnable = new Runnable() {
			public void run() {
				food_selection_listview.setEnabled(isEnable);
			}
		};
		
		return this.vi;
	}
	
	
	
	public void onResume(){
		super.onResume();

	}
	
	private void updateGUI(boolean isEnable) {
		FoodSelectingDialogFragment.isEnable=isEnable;
		myHandler.post(myRunnable);
	}
	
	/**
	 * Bu s�n�f i�erisinde yemek listesinden se�ilen yemeklere ait layout d�zenlenir.
	 * @author Okan ERDO�AN
	 * 31 May 2014
	 */
	class FoodListAdapter extends ArrayAdapter<FoodDataForm>{

		private Context context;
		private int resourceId;
		private ArrayList<FoodDataForm> arrayList;
		
		public FoodListAdapter(Context context, int textViewResourceId,ArrayList<FoodDataForm> objects) {
			super(context, textViewResourceId, objects);
			
			this.context = context;
			this.resourceId = textViewResourceId;
			
			
			this.arrayList = objects;
		}	
		
		public View getView(int position, View convertView, ViewGroup parent) {
			
			convertView = LayoutInflater.from(this.context).inflate(this.resourceId, null);
			
			FoodDataForm foodDataForm = (FoodDataForm) arrayList.get(position);
			LinearLayoutBase foodChoosingItemLinearlayout=(LinearLayoutBase) convertView.findViewById(R.id.food_choosing_item_linearlayout);
			
			TextViewBase foodType=(TextViewBase) convertView.findViewById(R.id.first_textview);
			foodType.setText(foodDataForm.getFoodName());
			
			TextViewBase specialty_name1=(TextViewBase) convertView.findViewById(R.id.specialty_name1);
			TextViewBase specialty_value1=(TextViewBase) convertView.findViewById(R.id.specialty_value1);

			
			for(int i=0;i<foodDataForm.getSelectablePortions().length;i++){
				if(foodDataForm.getSelectablePortions()[i]!=null){
					specialty_name1.setText(foodDataForm.getSelectablePortions()[i].getName());
					
					specialty_value1.setText(""+foodDataForm.getSelectablePortions()[i].getCount());
				}else{
					specialty_name1.setVisibility(View.GONE);
					specialty_value1.setVisibility(View.GONE);
				}
			}
			
			


			TextViewBase specialty_name2=(TextViewBase) convertView.findViewById(R.id.specialty_name2);
			TextViewBase specialty_value2=(TextViewBase) convertView.findViewById(R.id.specialty_value2);
			specialty_name2.setVisibility(View.GONE);
			specialty_value2.setVisibility(View.GONE);
			
			
			TextViewBase specialty_name3=(TextViewBase) convertView.findViewById(R.id.specialty_name3);
			TextViewBase specialty_value3=(TextViewBase) convertView.findViewById(R.id.specialty_value3);
			specialty_name3.setVisibility(View.GONE);
			specialty_value3.setVisibility(View.GONE);
			
			
			if(foodDataForm.getCount()!=0){
				foodChoosingItemLinearlayout.setBackgroundResource(R.drawable.item_bckgrnd3);
			}else{
				foodChoosingItemLinearlayout.setBackgroundResource(R.drawable.item_bckgrnd6);
			}
			
			return convertView;
		}
		
		public int getCount(){
			return arrayList.size();
		}
		
		public void notifyDataSetChanged() {
	        super.notifyDataSetChanged();
	        arrayList=foodArray;
	    }
		
	}
}
